package com.hn.vue.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.vue.mapper.RoleMapper;
import com.hn.vue.mapper.UserMapper;
import com.hn.vue.mapper.UserRoleMapper;
import com.hn.vue.param.UserParam;
import com.hn.vue.pojo.*;
import com.hn.vue.service.UserService;
import com.hn.vue.vo.PageVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * hanni
 * 2019/2/28 0028
 * 19:01
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public PageVo<User> findUserList(UserParam param) {

        PageHelper.startPage(param.getCPage(),param.getPageSize());
        List<User> uList = userMapper.findByParams(param);
        PageInfo<User> pageInfo = new PageInfo<>(uList);
        PageVo pageVo = new PageVo(pageInfo.getTotal(),pageInfo.getList());
        return pageVo;
    }

    @Override
    public void saveUser(User user) {
        if (user.getId()!=null){
            user.setLastUpdateTime(new Date());
            userMapper.updateByPrimaryKeySelective(user);

            UserRoleExample example  = new UserRoleExample();
            example.createCriteria().andUserIdEqualTo(user.getId());
            userRoleMapper.deleteByExample(example);
        }else{
            user.setStatus((byte)1);
            user.setCreateTime(new Date());
            user.setLastUpdateTime(new Date());
            user.setDelFlag((byte)0);
            user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));

//            user.setCreateBy();
//            user.setLastUpdateBy();
//            user.setSalt();
            userMapper.insert(user);
        }

        String roleids = user.getRoleIds();
        if(roleids!=null){
            String[] split = roleids.split(",");
            for (String s : split) {
                UserRole userRole= new UserRole();
                userRole.setRoleId(Long.parseLong(s));
                userRole.setUserId(user.getId());
//                userRole.setCreateBy();
//                userRole.setLastUpdateBy();
                userRole.setCreateTime(new Date());
                userRole.setLastUpdateTime(new Date());
                userRoleMapper.insert(userRole);

            }
        }

    }

    @Override
    public List<User> findAllUsers() {

        return userMapper.selectByExample(null);
    }

    @Override
    public User findUserByName(String username) {

//        UserExample example = new UserExample();
//        example.createCriteria().andNameEqualTo(username);
//        //按照名称查找是唯一的，就是说list结合里面只有一个元素
//        List<User> users = userMapper.selectByExample(example);
        List<User> users = userMapper.selectByName(username);

        return CollectionUtils.isEmpty(users) ? null : users.get(0);
    }

//    @Override
//    public void delete(String[] rowid) {
//        UserExample example = new UserExample();
//        example.createCriteria().andIdIn(Arrays.asList(rowid));
//        userMapper.deleteByExample();
//    }
}
